﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace LED_Tester
{
    public partial class frmLSecCalibration : Form
    {
        public frmLSecCalibration()
        {
            InitializeComponent();
        }

        #region 电源变量

        private ClassSourceMeter classSourceMeter;
        private int sourceMode;
        private SerialPort sptSource;
        private bool sourceIsJoin;
        private int preheatTime;
        private double voltSet;
        private double currSet;

        public ClassSourceMeter Class_SourceMeter
        {
            get { return classSourceMeter; }
            set { classSourceMeter = value; }
        }
        public int SourceMode
        {
            get { return sourceMode; }
            set { sourceMode = value; }
        }
        public SerialPort SPTSource
        {
            get { return sptSource; }
            set { sptSource = value; }
        }
        public bool SourceIsJoin
        {
            get { return sourceIsJoin; }
            set { sourceIsJoin = value; }
        }
        public int PreheatTime
        {
            get { return preheatTime; }
            set { preheatTime = value; }
        }
        public double VoltSet
        {
            get { return voltSet; }
            set { voltSet = value; }
        }
        public double CurrSet
        {
            get { return currSet; }
            set { currSet = value; }
        }

        #endregion

        #region 光谱仪变量

        private ClassSpectralMeter classSpectralMeter;
        private bool spectralIsJoin;
        private ClassSpectralMeter.Param param;
        private ClassSpectralMeter.Result result;

        public ClassSpectralMeter Class_SpectralMeter
        {
            get { return classSpectralMeter; }
            set { classSpectralMeter = value; }
        }
        public bool SpectralIsJoin
        {
            get { return spectralIsJoin; }
            set { spectralIsJoin = value; }
        }
        public ClassSpectralMeter.Param Param
        {
            get { return param; }
            set { param = value; }
        }
        public ClassSpectralMeter.Result Result
        {
            get { return result; }
            set { result = value; }
        }

        #endregion

        #region 光强板变量

        private ClassLuxBoard classLuxBoard;
        private bool luxIsJoin;
        private double distance;
        private double luxStanValue;
        private double luxDarkValue;

        public ClassLuxBoard Class_LuxBoard
        {
            get { return classLuxBoard; }
            set { classLuxBoard = value; }
        }
        public bool LuxIsJoin
        {
            get { return luxIsJoin; }
            set { luxIsJoin = value; }
        }
        public double Distance
        {
            get { return distance; }
            set { distance = value; }
        }
        public double LuxDarkValue
        {
            get { return luxDarkValue; }
            set { luxDarkValue = value; }
        }

        #endregion

        #region 其它变量

        private double luxParam;
        private int index;
        private delegate void dlgDisplay();
        private Thread threadMeasure;
        private DataGridViewTextBoxEditingControl editingControl = null;

        public double LuxParam
        {
            get { return luxParam; }
            set { luxParam = value; }
        }

        #endregion

        #region 功能函数

        private void initialize()
        {
            textBox1.Text = luxParam.ToString("#0.00");

            dgvShow.Rows.Clear();
            dgvInput.Rows.Clear();

            textBox2.Text = "";

            index = 1;
        }

        private void measure()
        {
            classSourceMeter.LightOn(sourceMode, sptSource);
            classSpectralMeter.MeasureCandela(param, ref luxStanValue);
            Thread.Sleep(300);
            Thread.Sleep(preheatTime);
            classSourceMeter.LightOff(sourceMode, sptSource);

            BeginInvoke(new dlgDisplay(display));
        }

        private void measAfterCali()
        {
            classSourceMeter.LightOn(sourceMode, sptSource);
            classSpectralMeter.MeasureCandela(param, ref luxStanValue);
            Thread.Sleep(300);
            Thread.Sleep(preheatTime);
            classSourceMeter.LightOff(sourceMode, sptSource);

            BeginInvoke(new dlgDisplay(displayAfterCali));
        }

        private void display()
        {
            DataGridViewRow rowShow = new DataGridViewRow();
            rowShow.CreateCells(dgvShow);

            rowShow.Cells[0].Value = "N-" + index.ToString("#0000");
            rowShow.Cells[1].Value = distance.ToString();
            rowShow.Cells[2].Value = luxStanValue.ToString("#0.00");

            dgvShow.Rows.Add(rowShow);

            DataGridViewRow rowIntput = new DataGridViewRow();
            rowIntput.CreateCells(dgvInput);

            dgvInput.Rows.Add(rowIntput);

            index++;
        }

        private void displayAfterCali()
        {
            luxStanValue *= luxParam;

            textBox2.Text = luxStanValue.ToString("#0.00");
        }
        // 用于数据验证1
        private static void dgvDataVerifyWithOnePoint(KeyPressEventArgs e, DataGridViewTextBoxEditingControl editingControl)
        {
            int j = 0;//用于存储'.'个数

            bool isPointExist = false;//用于判断'.'是否存在

            //判断输入的字符是否为数字、‘.‘、Backspace键
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;//操作有效
                if (editingControl.Text.Length == 0)
                {
                    //第一个输入的字符不能是','
                    if (e.KeyChar == '.')
                    {
                        e.Handled = true;//操作无效
                    }
                }
                for (int i = 0; i < editingControl.Text.Length; i++)
                {
                    if (editingControl.Text[i] == '.')
                    {
                        j++;
                        isPointExist = true;
                    }
                }
                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }
            else { e.Handled = true; }
        }
        // 用于数据验证2
        private void cell_KeyPress(object sender, KeyPressEventArgs e)
        {
            dgvDataVerifyWithOnePoint(e, editingControl);
        }

        #endregion

        #region 窗体事件

        private void frmLuminousSecCalibration_Load(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (!spectralIsJoin)
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            initialize();
        }

        #endregion

        #region 按钮事件

        private void btnRestore_Click(object sender, EventArgs e)
        {
            initialize();

            luxParam = 1;

            textBox1.Text = luxParam.ToString("#0.00");
        }

        private void btnMeasure_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!spectralIsJoin)
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            threadMeasure = new Thread(new ThreadStart(measure));
            threadMeasure.Start();
        }

        private void btnCalibration_Click(object sender, EventArgs e)
        {
            int n1 = dgvShow.Rows.Count;
            int n2 = dgvInput.Rows.Count;

            if (n1 == 0 || n2 == 0)
            {
                return;
            }
            else if (n1 == n2)
            {
                for (int i = 0; i < n1; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (dgvInput[j, i].Value == null)
                        {
                            dgvInput[j, i].Value = dgvShow[j, i].Value.ToString();
                        }
                    }
                }

                luxParam = 0;

                for (int i = 0; i < n1; i++)
                {
                    luxParam += double.Parse(dgvInput[2, i].Value.ToString()) / double.Parse(dgvShow[2, i].Value.ToString());
                }

                luxParam = luxParam / (n1);

                textBox1.Text = luxParam.ToString("#0.00");
            }
        }

        private void btnMeasAfterCali_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!spectralIsJoin)
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            threadMeasure = new Thread(new ThreadStart(measAfterCali));
            threadMeasure.Start();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        #endregion

        #region 其它事件

        private void dgvInput_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvInput.SelectedCells.Count <= 0)
            {
                return;
            }

            editingControl = (DataGridViewTextBoxEditingControl)e.Control;
            editingControl.SelectAll();
            editingControl.KeyPress += cell_KeyPress;
        }

        #endregion
    }
}
